var chart = {
    init:function (options) {
        if( options.type == 'line' ){
            chart.init_line(options);
        }
    },
    init_line:function(options){
        var canvasId = options.id + '_canvas';
        $('#'+options.id).append('<canvas id="'+canvasId+'" width="'+options.width+'" height="'+options.height+'"></canvas>');
        var ctx=document.getElementById(canvasId).getContext("2d");
        var partition = chart.getPartition(chart.getAllLineSeriesData(options.series));

        //x y 轴
        var start_x = 50.5,start_y = 20.5,end_y = options.height-50.5,end_x=options.width-20;
        ctx.moveTo(start_x,start_y);
        ctx.lineTo(start_x,end_y);
        ctx.lineTo(end_x,end_y);
        ctx.lineWidth = 1;//线条的宽度
        ctx.strokeStyle = "#000000";//线条的颜色
        ctx.stroke();

        //y轴 起始刻度
        ctx.moveTo(start_x,end_y);
        ctx.lineTo(start_x-5,end_y);
        ctx.lineTo(end_x,end_y);
        ctx.lineWidth = 1;//线条的宽度
        ctx.strokeStyle = "#000000";//线条的颜色
        ctx.stroke();
        ctx.fillText("0",start_x-15,end_y+3);

        // y轴 刻度
        for( var i=0 ; i<partition.length ; i++ ){
            var y = start_y+((end_y-start_y)/partition.length)*i;
            y = Math.floor(y)+0.5;

            //往左
            //ctx.beginPath();
            ctx.moveTo(start_x,y);
            ctx.lineTo(start_x-5,y);
            ctx.lineWidth = 1;//线条的宽度
            ctx.strokeStyle = "#000000";//线条的颜色
            ctx.stroke();
            ctx.beginPath();
            //往右

            ctx.moveTo(start_x,y);
            ctx.lineTo(end_x,y);
            ctx.lineWidth = 1;//线条的宽度
            ctx.strokeStyle = "#ADADAD";//线条的颜色
            ctx.stroke();
            ctx.beginPath();

            var scale = partition.topNumber/partition.length*(partition.length-i);
            ctx.fillText(scale+options.unit,start_x-20-(5*( (scale+options.unit).getBytesLength() - 2 )),y+3);
        }

        // x轴 刻度
        var xAxisData = options.xAxis.data;
        var xAvglength = (end_x-start_x)/(xAxisData.length-1);
        for( var i=0 ; i<xAxisData.length ; i++ ){
            var x = start_x+xAvglength*i;
            x = Math.floor(x)+0.5;
            if( i>0 ){
                ctx.moveTo(x,end_y);
                ctx.lineTo(x,end_y+5);
                ctx.lineWidth = 1;//线条的宽度
                ctx.strokeStyle = "#000000";//线条的颜色
                ctx.stroke();
            }

            ctx.font="13px Arial";
            ctx.fillText(xAxisData[i],x-10,end_y+20);
        }

        //数据
        //y轴刻度比例
        var params = {start_x:start_x,end_y:end_y,start_y:start_y,partition:partition,ctx:ctx,xAvglength:xAvglength};
        chart.drawLineSeries(options.series[0],params);
        chart.drawLineSeries(options.series[1],params);
    },
    getAllLineSeriesData:function(serie){
        var arr = [];
        for( var i=0 ; i<serie.length ; i++ ){
            var serieData = serie[i].data;
            for( var j=0 ; j<serieData.length ; j++ ){
                arr.push(serieData[j]);
            }
        }
        return arr;
    },
    drawLineSeries:function(serie,params){
        var seriesData = serie.data;
        var yscale = (params.end_y-params.start_y)/params.partition.topNumber;
        var points = [];
        params.ctx.beginPath();
        for( var i=0 ; i<seriesData.length ; i++ ){
            var y = params.end_y - yscale * seriesData[i];
            if( i==0 ){
                params.ctx.moveTo(params.start_x,y);
                points.push({x:params.start_x,y:y});
            }else{
                params.ctx.lineTo(params.start_x+params.xAvglength*i,y);
                points.push({x:params.start_x+params.xAvglength*i,y:y});
            }
        }
        params.ctx.lineWidth = 2;//线条的宽度
        params.ctx.strokeStyle = serie.color;//线条的颜色
        params.ctx.stroke();

        for( var i=0 ; i<points.length ; i++ ){
            params.ctx.beginPath();
            params.ctx.fillStyle=serie.color;
            params.ctx.arc(points[i].x,points[i].y,3,0,2*Math.PI);
            params.ctx.fill();
        }
    },
    getPartition:function (data) {
        // max length = 7    min length = 4
        var max = Math.max.apply(null,data);
        var topNumber,length;
        var multiple = 10;
        if( max > 1000 ){
            multiple = Math.pow(10,((max+'').length - 2 ));
        }
        for (var i=1 ; true ; i++){
            topNumber = (max - max % multiple + multiple * i);
            length = partitionLength(topNumber);
            if(length>0)break;
        }
        return {topNumber:topNumber,length:length};
    }
};

function partitionLength(topNumber) {
    for(var j=7 ; j>3 ; j--){
        if( topNumber%j==0){
            return j;
        }
    }
    return -1;
}

String.prototype.getBytesLength = function() {
    var totalLength = 0;
    var charCode;
    for (var i = 0; i < this.length; i++) {
        charCode = this.charCodeAt(i);
        if (charCode < 0x007f)  {
            totalLength++;
        } else if ((0x0080 <= charCode) && (charCode <= 0x07ff))  {
            totalLength += 2;
        } else if ((0x0800 <= charCode) && (charCode <= 0xffff))  {
            totalLength += 3;
        } else{
            totalLength += 4;
        }
    }
    return totalLength;
}